package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxBaoxiaodan;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyZxBaoxiaodanMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxBaoxiaodanParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyZxBaoxiaodanResult;
import  cn.stylefeng.guns.sys.modular.system.service.BtyZxBaoxiaodanService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxd
 * @since 2022-04-22
 */
@Service
public class BtyZxBaoxiaodanServiceImpl extends ServiceImpl<BtyZxBaoxiaodanMapper, BtyZxBaoxiaodan> implements BtyZxBaoxiaodanService {

    @Override
    public void add(BtyZxBaoxiaodanParam param){
        BtyZxBaoxiaodan entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyZxBaoxiaodanParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyZxBaoxiaodanParam param){
        BtyZxBaoxiaodan oldEntity = getOldEntity(param);
        BtyZxBaoxiaodan newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyZxBaoxiaodanResult findBySpec(BtyZxBaoxiaodanParam param){
        return null;
    }

    @Override
    public List<BtyZxBaoxiaodanResult> findListBySpec(BtyZxBaoxiaodanParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyZxBaoxiaodanParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BtyZxBaoxiaodanParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyZxBaoxiaodan getOldEntity(BtyZxBaoxiaodanParam param) {
        return this.getById(getKey(param));
    }

    private BtyZxBaoxiaodan getEntity(BtyZxBaoxiaodanParam param) {
        BtyZxBaoxiaodan entity = new BtyZxBaoxiaodan();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<BtyZxBaoxiaodanResult> ghsList(){
        QueryWrapper qw = new QueryWrapper();
        qw.select("max(user_id) as userId, user_name as userName");
        qw.groupBy("user_name");
        return this.baseMapper.selectList(qw);
    }
}
